package HomeWork3;

public class Store {
    private final int MAX=3;
    private int size=0;

    public synchronized void make() throws InterruptedException {
        String name = Thread.currentThread().getName();
        while (this.size>=MAX){
            System.out.println("桌上有：" + this.size + "个蛋糕，放不下了，" + name + "等待客人吃掉");
            wait();
        }
        this.size++;
        System.out.println(name + "做了个蛋糕，目前桌上有:" + this.size+"个");
        Thread.sleep(100);
        notifyAll();
    }

    public synchronized void eat() throws InterruptedException {
        String name = Thread.currentThread().getName();
        while (this.size<=0){
            System.out.println("目前桌上有："+this.size+"个蛋糕"+name+"等待厨师做蛋糕");
            wait();
        }
        this.size--;
        System.out.println(name+"吃了一个蛋糕，目前桌上有："+this.size+"个蛋糕");
        Thread.sleep(100);
        notifyAll();
    }

}
